import os
from pyftpdlib import ftpserver
import threading
from confignb import ConfigNB
from pyftpdlib.contrib.filesystems import UnixFilesystem

class Ftpd(threading.Thread):
    def __init__(self):
        try:
            threading.Thread.__init__(self,name = "ftpdnb")
        except:
            print "Imposible iniciar FTP Thread"
        
    def run(self):
        config = ConfigNB()
        ftpConfig = config.getFtpServerConfig()
        
        try:
            authorizer = ftpserver.DummyAuthorizer()
            authorizer.add_user(ftpConfig['user'], password=ftpConfig['password'], homedir=str(ftpConfig['path']), perm='elradfmw')
            
            handler = ftpserver.FTPHandler
            handler.authorizer = authorizer
            
            address = ('', 21)
            
            self.__server = ftpserver.FTPServer(address, handler)
            self.__server.serve_forever()
        except:
            print "Problemas al iniciar FTP"
            
    
    def stop(self):
        try:
            self.__server.close()
        except:
            print "Problemas al detener FTP"
